package com.smedi.ismedi.inmail.core.port.adapter.persistence.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smedi.ismedi.inmail.core.domain.repository.DingAttendanceResultRepository;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.model.DingAttendanceResultEntity;
import com.smedi.ismedi.inmail.core.port.adapter.persistence.mybatis.mapper.DingAttendanceResultMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @className: DingAttendanceResultRepositoryImpl
 * @Description: TODO
 * @version: v1.0.0
 * @author: wzx
 * @date: 2022/9/20 18:20
 */

@Repository
@RequiredArgsConstructor
@Slf4j
public class DingAttendanceResultRepositoryImpl extends ServiceImpl<DingAttendanceResultMapper, DingAttendanceResultEntity> implements DingAttendanceResultRepository, IService<DingAttendanceResultEntity> {


    @Override
    public void batchSave(List<DingAttendanceResultEntity> dingAttendanceResultEntityList) {
        try {
            this.baseMapper.batchSave(dingAttendanceResultEntityList);
        }catch (Exception e){
            if(e.getMessage().contains("Duplicate entry")){
                log.info(StringUtils.substringBefore(e.getMessage(),"for"));
            }else {
                log.error(e.getMessage());
            }

        }
    }


    @Override
    public List<DingAttendanceResultEntity> getRecordByUserIdAndWorkDate(List<String> userIds, String workDate) {
        LambdaQueryWrapper<DingAttendanceResultEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(DingAttendanceResultEntity::getUserId,userIds);
        lambdaQueryWrapper.apply("date_format(work_date,'%Y-%m') = {0}",workDate);
        lambdaQueryWrapper.ne(DingAttendanceResultEntity::getTimeResult,"NotSigned");
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }
}